#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10,M=1e6+10;
int T,n,a[N],flag[M],pre[N];
long long temp,ans,f[N];
void init()
{
    memset(flag,0,sizeof(flag));
    memset(pre,0,sizeof(pre));
    memset(f,0,sizeof(f));
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>T;
    while(T--)
    {
        init();
        ans=0;
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        for(int i=2;i<=n;i++)if(a[i]==a[i-1]&&a[i-1]!=a[i-2])ans+=a[i];
        for(int i=1;i<=n;i++)
        {
            if(flag[a[i]])pre[i]=flag[a[i]];
            else flag[a[i]]=i;
        }
        for(int i=1;i<=n;i++)
        {
            if(pre[i]){
                f[i]=max(f[i-1],a[i]+f[pre[i]-1]);
                if(pre[pre[i]+1]<pre[i]&&pre[pre[i]+1]!=0)f[i]=max(f[i],a[i]+f[pre[i]+1]);
            }
            else f[i]=f[i-1];
        }
        cout<<f[n]+ans<<'\n';
    }
    return 0;
}
